import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { List, Card, Input, Button, Modal, Form } from 'antd';
import moment from 'moment';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import Result from '@/components/Result';
import styles from './Team.less';

const FormItem = Form.Item;

@connect(({ team, loading }) => ({
  team,
  loading: loading.models.list,
}))
@Form.create()
class Team extends PureComponent {
  state = {
    visible: false,
    done: false,
    previewVisible: '',
  };

  formLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 13 },
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'team/fetch',
    });
  }

  showModal = () => {
    this.setState({
      visible: true,
      current: undefined,
    });
  };

  showEditModal = item => {
    this.setState({
      visible: true,
      current: item,
    });
  };

  handleDone = () => {
    setTimeout(() => this.addBtn.blur(), 0);
    this.setState({
      done: false,
      visible: false,
    });
  };

  handleCancels = () => this.setState({ previewVisible: false });

  handleCancel = () => {
    setTimeout(() => this.addBtn.blur(), 0);
    this.setState({
      visible: false,
    });
  };

  render() {
    const {
      team: { data },
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
    const { visible, done, previewVisible, current = {} } = this.state;

    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };

    const ListContent = ({
      data: {
        memberBaseCommission,
        type,
        createdAt,
        isSameGroupCommissionClassificationWithPreviousUser,
      },
    }) => (
      <div className={styles.listContent}>
        <div className={styles.listContentItem}>
          <span>基本佣金</span>
          <p>{memberBaseCommission}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>销售状态</span>
          <p>{type}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>会员</span>
          <p>
            {isSameGroupCommissionClassificationWithPreviousUser === 'different' ? '不同' : '同级'}
          </p>
        </div>
        <div className={styles.listContentItem}>
          <span>时间</span>
          <p>{moment(createdAt).format('YYYY-MM-DD')}</p>
        </div>
      </div>
    );

    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="已确认添加该图片"
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
            className={styles.formResult}
          />
        );
      }
      // const imageUrl = this.state.imageUrl;
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem visible={previewVisible} label="分类名称" {...this.formLayout}>
            {getFieldDecorator('name', {
              rules: [{ required: true, message: '请输入分类名称' }],
              initialValue: current.name,
            })(<Input placeholder="请输入" />)}
          </FormItem>
        </Form>
      );
    };
    return (
      <PageHeaderWrapper>
        <div className={styles.standardList}>
          <Card
            className={styles.listCard}
            bordered={false}
            title="日志管理"
            style={{ marginTop: 24 }}
            bodyStyle={{ padding: '0 32px 40px 32px' }}
          >
            {data ? (
              <List
                size="large"
                rowKey="id"
                loading={loading}
                dataSource={data}
                item={data}
                renderItem={item => (
                  <List.Item>
                    <List.Item.Meta title={`[${item.id}]`} description={item.intro} />
                    <ListContent data={item} />
                  </List.Item>
                )}
              />
            ) : (
              ''
            )}
          </Card>
        </div>
        <Modal
          title={done ? null : `分类${current ? '添加' : '编辑'}`}
          className={styles.standardListForm}
          width={640}
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
      </PageHeaderWrapper>
    );
  }
}

export default Team;
